package com.sibrary.booxoo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.Browser;
import android.provider.Browser.BookmarkColumns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class BooxooBookmarksActivity extends Activity {

	public class BookmarksData {

		byte[] mBookmarksFavicon;
		String mBookmarksTitle;
		String mBookmarksUrl;

		public BookmarksData() {
			// TODO Auto-generated constructor stub
		}
	}

	private List<BookmarksData> mBookmarks;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.booxoo_bookmarks_activity);

		getBookmarksData();

		ListView bookmarksListView = (ListView) findViewById(R.id.bookmarksListView);
		BookmarksAdapter bookmarksAdapter = new BookmarksAdapter(this,
				mBookmarks);
		bookmarksListView.setAdapter(bookmarksAdapter);

		bookmarksListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				TextView tv = (TextView) view.findViewById(R.id.bookmarksUrl);
				String bookmarksUrl = tv.getText().toString();

				// Toast.makeText(getApplicationContext(), bookmarksTitle,
				// Toast.LENGTH_SHORT).show();

				Intent intent = new Intent();
				intent.putExtra("bookmarksUrl", bookmarksUrl);
				setResult(BooxooMainActivity.BOOKMARKS_SHOW_REQUEST_CODE,
						intent);
				finish();
			}
		});
	}

	private void getBookmarksData() {
		mBookmarks = new ArrayList<BookmarksData>();

		ContentResolver contentResolver = getContentResolver();
		Cursor cursor = contentResolver.query(Browser.BOOKMARKS_URI, null,
				null, null, null);
		while (cursor.moveToNext()) {
			if (cursor.getString(
					cursor.getColumnIndex(Browser.BookmarkColumns.BOOKMARK))
					.equals("1")) {

				BookmarksData tmp = new BookmarksData();
				tmp.mBookmarksFavicon = cursor.getBlob(cursor
						.getColumnIndex(BookmarkColumns.FAVICON));
				// byte[] data = cursor.getBlob(cursor
				// .getColumnIndex(BookmarkColumns.FAVICON));
				tmp.mBookmarksTitle = cursor.getString(cursor
						.getColumnIndex(BookmarkColumns.TITLE));
				tmp.mBookmarksUrl = cursor.getString(cursor
						.getColumnIndex(BookmarkColumns.URL));
				mBookmarks.add(tmp);
			}
		}
	}

	public class BookmarksAdapter extends BaseAdapter {

		private Context context;

		// 数据源
		private List<BookmarksData> mAdapterBookmarks;

		// 构造函数
		public BookmarksAdapter(Context context, List<BookmarksData> bookmarks) {
			this.context = context;
			this.mAdapterBookmarks = bookmarks;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mAdapterBookmarks.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mAdapterBookmarks.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			// 获取数据
			BookmarksData tmp = mAdapterBookmarks.get(position);

			ViewHolder holder = null;

			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.booxoo_bookmarks_item, null);

				holder = new ViewHolder();

				holder.mTitleTextView = (TextView) convertView
						.findViewById(R.id.bookmarksTitle);

				holder.mUrlTextView = (TextView) convertView
						.findViewById(R.id.bookmarksUrl);

				holder.mFaviconImageView = (ImageView) convertView
						.findViewById(R.id.bookmarksIcon);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.mTitleTextView.setText(tmp.mBookmarksTitle);
			holder.mUrlTextView.setText(tmp.mBookmarksUrl);
			if (tmp.mBookmarksFavicon != null) {
				setFavicon(holder.mFaviconImageView,
						BitmapFactory.decodeByteArray(tmp.mBookmarksFavicon, 0,
								tmp.mBookmarksFavicon.length));
			} else {
				setFavicon(holder.mFaviconImageView, null);
			}
			return convertView;
		}

		public class ViewHolder {
			public ImageView mFaviconImageView;
			public TextView mTitleTextView;
			public TextView mUrlTextView;
		}

		private void setFavicon(ImageView view, Bitmap b) {
			if (b != null) {
				view.setImageBitmap(b);
			} else {
				view.setImageResource(R.drawable.bookmarks_favicon_default);
			}
		}
	}
}
